package com.sf.osms.dep.processor.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.AgentFileMap;
import com.sf.osms.dep.domain.Handover;
import com.sf.osms.dep.domain.RouterProblemRecord;
import com.sf.osms.dep.processor.exception.RouterException;
import com.sf.osms.dep.processor.model.EventModel;
import com.sf.osms.dep.processor.util.CollectionUtil;
import com.sf.osms.dep.processor.util.ExceptionUtils;
import com.sf.osms.dep.processor.util.HandoverLine;
import com.sf.osms.dep.processor.util.ReflectUtils;

/**
 * Handover处理
 * @author 841245
 *
 */
public class HandoverHandler {

	private final static Logger logger = Logger.getLogger(HandoverHandler.class);
	
	public EventModel<HandoverLine, Handover> handle(EventModel<HandoverLine,Handover> eventModel,
			Agent agentInfo) throws RouterException {
		
		if(null == eventModel || null == agentInfo){
			return null;
		}
		//得到需要进行处理的原始数据集合
		List<HandoverLine> orginalRecords = eventModel.getRecordList();
		
		//创建处理成为Router的集合
		List<Handover> correctRouter = new ArrayList<Handover>();
		eventModel.setCorrectRouter(correctRouter);
		
		//错误处理的集合
		List<RouterProblemRecord> errorRecords = new ArrayList<RouterProblemRecord>();
		eventModel.setErrorEventRecord(errorRecords);
		
		Handover error;
		if(!CollectionUtil.isEmpty(orginalRecords)){
			for (int i = 0 ; i < orginalRecords.size() ; i++) {
				HandoverLine record =  orginalRecords.get(i);
				try {
					if (record==null || record.getLineData() == null ||record.getLineData().length==0) {
						error = new Handover();
						error.setFailReason("zh_CN".equals("zh_CN")?"空白行":"empty line");
						error.setLineNum(record.getLineNum());
						eventModel.getProblemHandoverList().add(error);
						continue;
					}
					convertToHandler(eventModel,record,agentInfo); //原始数据转换为Router
				} catch (Exception ex){
					logger.error("处理" + eventModel.getFile().getName() + "中的数据出现异常");
					logger.error(ExceptionUtils.getExceptionStackString(ex));
				}
			}
			
		}
		
		return eventModel;
	}
	
	
	
	/**
	 * 将解析出来的原始数据转化为Router
	 * 
	 * @param eventModel
	 * @param record
	 * @param agentInfo
	 * @throws RouterException
	 */
	public void convertToHandler(EventModel<HandoverLine, Handover> eventModel, HandoverLine record,
			Agent agentInfo) throws RouterException {

		AgentFileMap parseRules = agentInfo.getAgentFileMap();
		if (null == parseRules) {
			return;
		}
		String mapRules = parseRules.getMapRule();
		Map<String, String> routerFieldAndStr = null;

		Map<String, String> newMapRules = CollectionUtil
				.formatRuleToMap(mapRules); // 字段对应规则,必须是一一对应
		Map<String, String> fieldValues = CollectionUtil
				.transferObjectToMap(record.getLineData()); // 字段--字段值（Map形式）
		// 得到Router表中属性和属性对应的String类型的值
		routerFieldAndStr = CollectionUtil.getFileldAndValueMap(newMapRules,
				fieldValues);
		
		Set<String> routerFileds = routerFieldAndStr.keySet();
		Handover handover = new Handover();
		handover.setLineNum(record.getLineNum());
		for (String fieldStr : routerFileds) {
			String filedVal = routerFieldAndStr.get(fieldStr).trim();
			if(StringUtils.isEmpty(filedVal)){
				continue;
			}
			
			ReflectUtils.setFiledValue(handover,fieldStr,filedVal);
		}
		eventModel.getCorrectRouter().add(handover);
		

	}
	
	
}
